package view;

import DAO.ClassDAO;
import DAO.CourseDAO;
import DAO.ScheduleDAO;
import java.awt.event.ItemEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.ClassModel;
import model.Course;
import model.Schedule;
import model.Staff;
import model.Subject;

public class ViewAddClassSchedule extends javax.swing.JDialog {

    public static DefaultTableModel model;
    String[] colSubject = {"Subject ID", "Subject Name", "Session", "Start Time", "End Time", "Staff"};
    private ClassModel clm;

    public ViewAddClassSchedule(java.awt.Frame parent, boolean modal, ClassModel clm) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        this.clm = clm;
        lblClass.setText(clm.getClassName());
        loadSubject();
    }

    public void loadSubject() {
        model = new DefaultTableModel();
        model.setColumnIdentifiers(colSubject);
        tblSubject.setModel(model);
    }

    public static List<Subject> getListSubject() {
        List<Subject> list = new ArrayList<Subject>();
        for (int i = 0; i < model.getRowCount(); i++) {
            Subject sbj = new Subject();
            sbj = (Subject) model.getValueAt(i, 1);
            list.add(sbj);
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jDate = new com.toedter.calendar.JDateChooser();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSubject = new javax.swing.JTable();
        btnAddsubject = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        tblAddSchedule = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        lblClass = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Class Name :");

        jLabel3.setText("Date :");

        jDate.setDateFormatString("dd/MM/yyyy");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        tblSubject.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Subject ID", "Subject Name ", "Start Time", "End Time", "Session", "Staff"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblSubject);
        if (tblSubject.getColumnModel().getColumnCount() > 0) {
            tblSubject.getColumnModel().getColumn(0).setResizable(false);
            tblSubject.getColumnModel().getColumn(1).setResizable(false);
            tblSubject.getColumnModel().getColumn(2).setResizable(false);
            tblSubject.getColumnModel().getColumn(3).setResizable(false);
            tblSubject.getColumnModel().getColumn(4).setResizable(false);
            tblSubject.getColumnModel().getColumn(5).setResizable(false);
        }

        btnAddsubject.setText("Add Subject");
        btnAddsubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddsubjectActionPerformed(evt);
            }
        });

        jButton1.setText("Remove Subject");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAddsubject)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddsubject)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        tblAddSchedule.setText("Add Class Schedule");
        tblAddSchedule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tblAddScheduleActionPerformed(evt);
            }
        });

        jLabel2.setText("Subject List :");

        lblClass.setText("jLabel6");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jDate, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                            .addComponent(lblClass, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(216, 216, 216)
                        .addComponent(tblAddSchedule)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblClass))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(tblAddSchedule)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        jLabel4.setText("Add Class Schedule");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(249, 249, 249)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddsubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddsubjectActionPerformed
        try {
            SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String date = mySimpleDateFormat.format(jDate.getCalendar().getTime());
            if (ScheduleDAO.checkScheduleByDate(clm.getClassId(), date)) {
                JOptionPane.showMessageDialog(this, "ngay nay hoc roi, chon ngay khac");
            } else {
                new ViewAddSubjectSchedule(null, true, clm,date).setVisible(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "birthday fail");
        }
    }//GEN-LAST:event_btnAddsubjectActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int tb = tblSubject.getSelectedRow();
        if (tb == -1) {
            JOptionPane.showMessageDialog(this, "select a row");
        } else {
            model.removeRow(tb);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tblAddScheduleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tblAddScheduleActionPerformed
        try {
            SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String date = mySimpleDateFormat.format(jDate.getCalendar().getTime());
            if (ScheduleDAO.checkScheduleByDate(clm.getClassId(), date)) {
                JOptionPane.showMessageDialog(this, "ngay nay hoc roi, chon ngay khac");
            } else {
                if (model.getRowCount() > 0) {
                    List<Schedule> listSchedule = new ArrayList<Schedule>();
                    for (int i = 0; i < model.getRowCount(); i++) {
                        Subject sj = (Subject) tblSubject.getValueAt(i, 1);
                        int session = Integer.parseInt(tblSubject.getValueAt(i, 2).toString());
                        String startTime = tblSubject.getValueAt(i, 3).toString();
                        String endTime = tblSubject.getValueAt(i, 4).toString();
                        Staff staff = (Staff) tblSubject.getValueAt(i, 5);
                        Schedule schedule = new Schedule();
                        schedule.setClassModel(clm);
                        schedule.setSubject(sj);
                        schedule.setSession(session);
                        schedule.setStartTime(startTime);
                        schedule.setEndTime(endTime);
                        schedule.setDate(date);
                        schedule.setStaff(staff);
                        listSchedule.add(schedule);
                    }
                    boolean rs = ScheduleDAO.addSchedule(listSchedule);
                    if (rs) {
                        JOptionPane.showMessageDialog(this, "Thanh cong");
                        ((ViewStaff) this.getParent()).loadTableClass();
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(this, "That bai");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "Select subject");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "birth day ...fail");
        }


    }//GEN-LAST:event_tblAddScheduleActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddsubject;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblClass;
    private javax.swing.JButton tblAddSchedule;
    public static javax.swing.JTable tblSubject;
    // End of variables declaration//GEN-END:variables
}
